<?php
declare (strict_types=1);

namespace app\index\controller;

use app\model\Cdkey;
use app\model\Config;
use app\model\Income;
use app\model\Invite;
use app\model\Notice;
use app\model\Profit;
use app\model\Profitlog;
use app\model\Transfer;
use app\model\TransferList;
use app\model\User;
use think\facade\Cache;
use think\facade\Db;
use think\Request;

/**
 * 新增api
 */
class Api3 extends ApiBase
{

    /**
     * 获取公告
     */
    public function notice() {
        $notice = Notice::where("id", 1)->field('content')->find();
        return success($notice);
    }

    /**
     * User: 肥茹人士
     * Time: 2023/9/17 21:07
     * Notes:公告列表
     */
    public function noticeList()
    {
        $page = $this->request->param("page") ? $this->request->param("page") : 1;
        $page = intval($page);
        $limit = $this->request->param("limit") ? $this->request->param("limit") : 1;
        $limit = intval($limit);
        $start = $limit * ($page - 1);
        $where = [];
        // 搜索条件判断结束
//        $result['count'] = Notice::count();//->limit($start, $limit)
        $list = Notice::where($where)->order('id desc')->select();
        return success($list);
    }


    //获取邀请信息
    public function getInvite()
    {
        $u = $this->user;

        //邀请人数
        $invite = User::where('invite', $u['id'])->field('id as form_id,username')->select()->toArray();
        $inviteNum =count($invite);

        //邀请收益
        $inviteIncome = Profitlog::where('uid', $u['id'])
            ->where('from_id', '>', 0)
            ->sum('money')??0;

        // 今日收益
        $todayInviteIncome = Profitlog::where('uid', $u['id'])
            ->where('from_id', '>', 0)
            ->where("date", "like", date("Y-m-d") . "%")
            ->sum('money')??0;

        // 昨日收益
        $yestodayInviteIncome  = Profitlog::where('uid', $u['id'])
            ->where('from_id', '>', 0)
            ->where("date", "like", date("Y-m-d", strtotime("-1 day")) . "%")
            ->sum('money')??0;

        return success([
            // 'inviteList'   => $invite,
            'inviteNum'    => $inviteNum,
            'inviteIncome' => $inviteIncome,
            'today_invite_income' => $todayInviteIncome,
            'yestoday_invite_income' => $yestodayInviteIncome
        ]);

    }

    //获取邀请信息详情
    public function getInviteInfo()
    {
        $page = input('page', 1);
        $limit = input('limit', 10);
        $from_id = input('from_id');
        $where = [];
        $where[] = ['from_id', '=', $from_id];

        $list = Db::name('profit_log')
            ->where($where)
            ->order('id desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ],false,['query'=>request()->param()])->toArray();


        return success($list['data']);

    }












}
